/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditCourse.java
 *
 * Created on 31-05-2011, 02:05:05
 */
package UpdateForm;

import ConnectData.Config;
import ConnectData.getConnections;
import Management.AdminForm;
import UploadData.Database;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author s0lo
 */
public class EditCourse extends javax.swing.JDialog {

    private String CourseName;
     private String TotalFees;
    private int CourseID;
    private Vector vtSubject;
    private Vector vtSubject2;
    private Vector vtSubjectDetail;
    /** Creates new form AddSpeciality */
    AdminForm parent;

    public EditCourse(java.awt.Frame parent, boolean modal, int CourID, String CourName,String totalFees) {
        super(parent, modal);
        initComponents();
        this.CourseID = CourID;
        this.CourseName = CourName;
        this.TotalFees= totalFees;
        txtCourseName.setText(CourName);
        txtTotalFees.setText(totalFees);
        initSubject();
        this.parent = (AdminForm) parent;
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public void initSubject() {
        vtSubject = new Database().loadSubject("");
        if (vtSubject.size() != 0) {
            for (int l = 0; l < vtSubject.size(); l++) {
                String subname = ((Vector) vtSubject.get(l)).get(1).toString();
                vtSubjectDetail = new Database().loadSubjectDetail(" where CourseID=" + CourseID + " order by [CourseID]");
                if (vtSubjectDetail.size() != 0) {
                    for (int i = 0; i < vtSubjectDetail.size(); i++) {
                        int subid = Integer.parseInt(((Vector) vtSubjectDetail.get(i)).get(0).toString());
                        vtSubject2 = new Database().loadSubject(" where SubID=" + subid + " order by [SubName]");
                        for (int j = 0; j < vtSubject2.size(); j++) {
                            String subName2 = ((Vector) vtSubject2.get(j)).get(1).toString();
                            if (subName2.toString().equals(subname)) {
                                cblSubject.add(subName2, true);
                                break;
                            }
                        }
                    }
                }
                if (!cblSubject.getItems().contains(subname.toString())) {
                    cblSubject.add(subname, false);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        cblSubject = new creator.components.CheckBoxList();
        jLabel2 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        txtCourseName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtTotalFees = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 20));
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("              EDIT COURSE");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addContainerGap(99, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnClose.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnClose.setForeground(new java.awt.Color(0, 0, 51));
        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/close1.png"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        cblSubject.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cblSubjectMouseClicked(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(new java.awt.Color(0, 0, 51));
        jLabel2.setText("Subject :");

        btnSave.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        txtCourseName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCourseNameActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(0, 0, 51));
        jLabel1.setText("Course Name : ");

        txtTotalFees.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTotalFeesActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setForeground(new java.awt.Color(0, 0, 51));
        jLabel4.setText("Total Fees :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnClose)
                .addGap(92, 92, 92))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cblSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTotalFees, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCourseName, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCourseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtTotalFees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(cblSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnSave))
                .addGap(43, 43, 43))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    
        boolean valid = validateInformation();
        if (valid) {
            String sn = txtCourseName.getText();
            String totalFees = txtTotalFees.getText();
            Connection cn = new Config().cn;
            PreparedStatement pst = null;
            try {
                String strdel = "delete from SubjectDetail where CourseID=?";
                pst = cn.prepareCall(strdel);
                pst.setInt(1, CourseID);
                pst.executeUpdate();
                Vector listCour = cblSubject.SelectedItems();
                int subID;
                int kq = -1;
                if (listCour.size() > 0) {
                    for (int i = 0; i < listCour.size(); i++) {
                        String SubName = listCour.get(i).toString();
                        Vector vtSpe = new Database().loadSubject("where SubName='" + SubName + "' order by [SubName]");
                        if (vtSpe.size() > 0) {
                            String subName;
                            for (int j = 0; j < vtSpe.size(); j++) {
                                subName = ((Vector) vtSpe.get(j)).get(1).toString();
                                if (SubName.equals(subName)) {
                                    subID = Integer.parseInt(((Vector) vtSpe.get(j)).get(0).toString());
                                    String InsSubDe = "INSERT INTO SubjectDetail(SubID,CourseID) VALUES(?,?)";
                                    pst = cn.prepareCall(InsSubDe);
                                    pst.setInt(1, subID);
                                    pst.setInt(2, CourseID);
                                    kq = pst.executeUpdate();
                                }
                            }
                        }
                    }
                }
                String strUpd = "Update Course set CourseName=?,TotalFees=? where CourseID=?";
                pst = cn.prepareCall(strUpd);
                pst.setString(1, sn);
                pst.setString(2, txtTotalFees.getText());
                pst.setInt(3, CourseID);
                pst.executeUpdate();
                if (kq != -1) {
                    JOptionPane.showMessageDialog(this, "Update successful!");
                    parent.loadCourse();
                    this.dispose();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    getConnections.closeObject(pst);
                    getConnections.closeObject(cn);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
     
}//GEN-LAST:event_btnSaveActionPerformed

    private void txtCourseNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCourseNameActionPerformed
        btnSaveActionPerformed(evt);
}//GEN-LAST:event_txtCourseNameActionPerformed

    private void cblSubjectMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cblSubjectMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_cblSubjectMouseClicked

    private void txtTotalFeesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTotalFeesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTotalFeesActionPerformed

    /**
     * @param args the command line arguments
     */
    public boolean validateInformation() {
        CourseName = txtCourseName.getText();
        if (CourseName.equals("")) {
            JOptionPane.showMessageDialog(this, "you must input Course Name!");
            txtCourseName.requestFocus();
            return false;
        }
        Vector vtSick = cblSubject.SelectedItems();
        if (vtSick.size() <= 0) {
            JOptionPane.showMessageDialog(this, "you must check Subject!");
            cblSubject.requestFocus();
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private creator.components.CheckBoxList cblSubject;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtCourseName;
    private javax.swing.JTextField txtTotalFees;
    // End of variables declaration//GEN-END:variables
}
